package org.xx.armory.swing.builders;

import org.apache.commons.lang3.StringUtils;
import org.xx.armory.objectparser.ParsedObjectBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class UIModelBuilder
        extends ParsedObjectBuilder<UIModel>
        implements ContainerModelBuilder {
    private final Map<String, String> messages;
    private final List<ImageModel> images;
    private final List<OptionsModel> options;
    private final List<String> standardActions;
    private final List<ActionModel> actions;
    private final List<MenuBarModel> menuBars;
    private final List<ToolBarModel> toolBars;
    private final List<ComponentModel> children;
    private String resourceName;
    private String title;
    private int width;
    private int height;
    private String icon;
    private int margin;

    public UIModelBuilder() {
        this.resourceName = "";
        this.title = "";
        this.icon = "";
        this.messages = new HashMap<>();
        this.images = new ArrayList<>();
        this.options = new ArrayList<>();
        this.standardActions = new ArrayList<>();
        this.actions = new ArrayList<>();
        this.menuBars = new ArrayList<>();
        this.toolBars = new ArrayList<>();
        this.children = new ArrayList<>();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public UIModel build() {
        return new UIModel(getLineInfo(),
                           this.resourceName,
                           this.title,
                           this.width,
                           this.height,
                           this.icon,
                           this.margin,
                           this.messages,
                           this.images,
                           this.options,
                           this.standardActions.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList()),
                           this.actions.stream().filter(Objects::nonNull).collect(Collectors.toList()),
                           this.menuBars.stream().filter(Objects::nonNull).collect(Collectors.toList()),
                           this.toolBars.stream().filter(Objects::nonNull).collect(Collectors.toList()),
                           this.children.stream().filter(Objects::nonNull).collect(Collectors.toList()));
    }

    public final UIModelBuilder setResourceName(
            String resourceName
    ) {
        this.resourceName = resourceName;
        return this;
    }

    public final UIModelBuilder setTitle(
            String title
    ) {
        this.title = title;
        return this;
    }

    public final UIModelBuilder setWidth(
            int width
    ) {
        this.width = width;
        return this;
    }

    public final UIModelBuilder setHeight(
            int height
    ) {
        this.height = height;
        return this;
    }

    public final UIModelBuilder setIcon(
            String icon
    ) {
        this.icon = icon;
        return this;
    }

    public final UIModelBuilder setMargin(
            int margin
    ) {
        this.margin = margin;
        return this;
    }

    public final UIModelBuilder addMessage(
            String name,
            String value
    ) {
        this.messages.put(name, value);
        return this;
    }

    public final UIModelBuilder addImage(
            ImageModel value
    ) {
        this.images.add(value);
        return this;
    }

    public final UIModelBuilder addOptions(
            OptionsModel value
    ) {
        this.options.add(value);
        return this;
    }

    public final UIModelBuilder addStandardAction(
            String actionName
    ) {
        this.standardActions.add(actionName);
        return this;
    }

    public final UIModelBuilder addAction(
            ActionModel action
    ) {
        this.actions.add(action);
        return this;
    }

    public final UIModelBuilder addMenuBar(
            MenuBarModel menuBar
    ) {
        this.menuBars.add(menuBar);
        return this;
    }

    public final UIModelBuilder addTooBar(
            ToolBarModel toolBar
    ) {
        this.toolBars.add(toolBar);
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final UIModelBuilder addChild(
            ComponentModel child
    ) {
        this.children.add(child);
        return this;
    }
}
